<?php
	header('Content-type: text/json;charset=utf-8');
	require_once("json.php");
	error_reporting(E_ALL ^ E_DEPRECATED);
		
	$cellphone = $_POST['cellphone'];
	$comment = $_POST['comment'];
	$is_admin = $_POST['is_admin'];
	$returnArray = array();
	
	define('DB_HOST',localhost);
	define('DB_USER',root);
	define('DB_PWD',sa);
	define('DB_NAME',tags);
	$con =mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
	if (!$con)
    {
  		$returnArray['status']="success";
  		$returnArray['msg']='Could not connect: ' . mysql_error();
  		$resJson = json_encode($returnArray);
		echo $resJson."\n";
  		return 0;
  	}
	mysql_select_db("app_tagwall", $con);
	mysql_query("set names utf8");
	
	if($is_admin == "1") {
        $sel = "select is_admin from tmc_tag where cellphone='$cellphone' and is_admin=$is_admin limit 1";
        $returnArray['sel']=$sel;
        $result=mysql_query( $sel, $con);
        $record = mysql_fetch_array($result);
        if ($record) {
            $returnArray['status']="success";
            $selc = "select * from tmc_tag where cellphone='$cellphone' and comment='$comment' limit 1";
            $returnArray['selc']=$selc;
            $result1=mysql_query( $selc, $con);
            $record1 = mysql_fetch_array($result1);
            if($record1) {
               $returnArray['status']="failed";
                $returnArray['msg']="You cannot add two same comments"; 
            } else {
               $dt = date('y-m-d h:i:s',time());
               $seli = "insert into tmc_tag values(null,$is_admin,0,0,'$cellphone','$dt','$comment')"; 
                $returnArray['seli']=$seli;
               $result2=mysql_query( $seli, $con);
               if($result2) {
                  $returnArray['status']="success"; 
                  $returnArray['msg']="Thanks for your comment, waiting for admin verify it";
               }
                
            }
        } else {
            $returnArray['status']="failed";
            $returnArray['msg']="You are not administrator";
        }
    } else {
        $selc = "select * from tmc_tag where cellphone='$cellphone' and comment='$comment' limit 1";
        $returnArray['selc']=$selc;
        $result1=mysql_query( $selc, $con);
        $record1 = mysql_fetch_array($result1);
        if($record1) {
            $returnArray['status']="failed";
            $returnArray['msg']="You cannot add two same comments"; 
        } else {
            $dt = date('y-m-d h:i:s',time());
            $seli = "insert into tmc_tag values(null,$is_admin,0,0,'$cellphone','$dt','$comment')"; 
            $returnArray['seli']=$seli;
            $result2=mysql_query( $seli, $con);
            if($result2) {
                $returnArray['status']="success"; 
                $returnArray['msg']="Thanks for your comment, waiting for admin verify it";
            } else {
                $returnArray['status']="failed"; 
                $returnArray['msg']="Add comment failed!";
            }
            
        }
    }
	mysql_close($con);
	$resJson = json_encode($returnArray);
	echo $resJson."\n";

?>